package com.sphd.restaurantsystem.model;


import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.base.RestaurantsApplication;
import com.sphd.restaurantsystem.interfaces.VolleyStringCallBack;
import com.sphd.restaurantsystem.presenter.StaffInfoPresenterImpl;
import com.sphd.restaurantsystem.utils.upload.MyMultipartRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by Hu on 2016/12/23
 */

public class StaffInfoModelImpl implements StaffInfoModel,VolleyStringCallBack {
    StaffInfoPresenterImpl staffInfoPresenter;
    @Override
    public void sendMap(String url, Map<String, String> map, StaffInfoPresenterImpl staffInfoPresenter) {
        this.staffInfoPresenter=staffInfoPresenter;
        volleyUtil.setStringRequest(url, this, map);
    }

    @Override
    public void sendMapAndFile(String url, String imageName, List<File> fileList, Map<String, String> map, StaffInfoPresenterImpl staffInfoPresenter) {
        this.staffInfoPresenter=staffInfoPresenter;
        MyMultipartRequest multipartRequest = new MyMultipartRequest(url,
                new Response.Listener() {
                    @Override
                    public void onResponse(Object response) {
                        getRes(response.toString());
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                staffInfoPresenter.sendError(error);
            }
        },imageName, fileList, map);

        RestaurantsApplication.getRequestQueue().add(multipartRequest);
    }



    @Override
    public void onResponse(String response) {
        getRes(response);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        staffInfoPresenter.sendError(error);
    }

    private void getRes(String response){
        try {
            int res=new JSONObject(response.toString()).getInt("res");
            staffInfoPresenter.recRes(res);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}